package logic.ui.uieditscene
{
	import com.senocular.display.TransformTool;
	
	import common.core.GEevnt;
	import common.core.GMouse;
	import common.core.GSprite;
	import common.manager.GUI;
	import common.manager.ObserverEvt;
	import common.manager.Resource;
	
	import component.ComponentLib;
	import component.hide.FileReference2;
	import component.show.containers.Image;
	import component.show.containers.UIFrame;
	import component.show.control.UISprite;
	import component.untils.ComponentItem;
	import component.untils.ComponentXMLAdapter;
	
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import fl.controls.ComboBox;
	import fl.controls.Label;
	import fl.controls.List;
	import fl.core.UIComponent;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import logic.cmd.OpenCompoentInfoMedCMD;
	import logic.cmd.OpenUIXMLCMD;
	import logic.cmd.SaveUIXMLCMD;
	import logic.control.ComponentMoveControl;
	import logic.def.Assets;
	import logic.def.Def;
	import logic.ui.cell.ScanComponent_LstCell;
	import logic.ui.componentInfoMed.ImageInfoMed;
	
	import untils.TransformTool2;

	public class UIContainMed extends GSprite
	{
		public static const NAME : String = "UIContainMed";
		
		public static const ROOTFRAMEGROUPINFO : String = "UIContainMed_RootFrameGroupInfo";
		public static const COMPONENTCONTROLACTIVE : String = "UIContainMed_ComponentControlActiove";
		
		private var defaultTool:TransformTool2;
		private var currTool:TransformTool2;
		
		private var m_uicontain : ScrollPane;
		private var m_uiLayer : GSprite;
		
		private var m_componentXML : XML;
		private var m_componentRootItem : ComponentItem;
		private var m_componentGroup : Array;
		private var m_uiRootFrameGroup : Vector.<ComponentItem>;
		
		private var m_componentXMLApataer : ComponentXMLAdapter;
		
		private var m_addComponent : Boolean;
		
		private var m_targetComMoveControl : ComponentMoveControl;
		
		public function UIContainMed()
		{
			m_uicontain = new ScrollPane;
			m_uiLayer = new GSprite;
			m_targetComMoveControl = new ComponentMoveControl(null,1,1);
			
			m_uicontain.width = 800;
			m_uicontain.height = 600;
			
			m_uicontain.source = m_uiLayer;
			
			defaultTool = new TransformTool2();
			addChild(defaultTool);
			
			m_componentRootItem = new ComponentItem; 
			m_componentGroup = new Array;
			m_uiRootFrameGroup = new Vector.<ComponentItem>;
			m_componentXMLApataer = new ComponentXMLAdapter;
			
			m_componentRootItem.item = m_uiLayer;
			
			var ScaleCircle : Class = Assets.ScaleCircle;
			
			
			m_uicontain.opaqueBackground = 0x8F8F8F;
			
			m_uiLayer.width = m_uicontain.width;
			m_uiLayer.height = m_uicontain.height;
			
			currTool = defaultTool;
			
			m_uiLayer.addEventListener(MouseEvent.MOUSE_DOWN, onSelectComponent);
			
			currTool.addEventListener(TransformTool2.CHANGED,updateItem);
			
			m_uiLayer.addEventListener(MouseEvent.MOUSE_OVER,onMouseOverUIContain);
			m_uiLayer.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutUIContain);
			
			this.addChild(m_uicontain);
			
			m_uicontain.invalidate();
		}
		
		public override function destory() : void
		{
			m_uiLayer.removeEventListener(MouseEvent.MOUSE_DOWN, onSelectComponent);
			
			currTool.removeEventListener(TransformTool2.CHANGED,updateItem);
			
			m_uiLayer.removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverUIContain);
			m_uiLayer.removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutUIContain);
			
			for(var i:int=0;i<m_componentGroup.length;i++)
			{
				if(m_componentGroup[i].hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					m_componentGroup[i].removeEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent);
				}
			}
			
			m_componentXMLApataer.destory();
			
			defaultTool = null;
			currTool = null;
			m_uicontain = null;
			m_uiLayer = null;
			m_componentGroup = null;
			m_componentRootItem = null;
			m_componentXMLApataer = null;
		}
		
		/**
		 * 选中组件
		 * @param event
		 */		
		private function onSelectComponent(event : MouseEvent):void{
			
			event.stopPropagation();
			
			var currentTarget:* = event.currentTarget;
			
			if (event.currentTarget is Stage) 
			{
				currTool.target = null;
				m_targetComMoveControl.target = null;
			}
			else if (event.currentTarget is DisplayObject)
			{
				m_addComponent = true;
				
				if(currentTarget != m_uiLayer)
				{
					var length : int = m_componentGroup.length;
					
					for(var i:int = 0;i<length;i++)
					{
						if(m_componentGroup[i].item == currentTarget)
						{
							setOperTarget(m_componentGroup[i]);
							
							break;
						}
					}
				}
				else
				{
					currTool.target = null;
					m_targetComMoveControl.target = null;
					ObserverEvt.Send(OpenCompoentInfoMedCMD.OPENCOMPENTINFOMED,null);
				}
				
				if(GMouse.handData)
				{
					var compon : Sprite = new ComponentLib.lib[GMouse.handData["label"]];
					
					compon.x = currentTarget.mouseX;
					compon.y = currentTarget.mouseY;
					
					currentTarget.addChild(compon);
					m_uicontain.update();
					
					GMouse.showHand(null);
					
					var item : ComponentItem = new ComponentItem;
					item.item = compon;
					
					compon.width = compon.width;
					compon.height = compon.height;
					
					if(compon.parent == m_uiLayer && compon is UIFrame)
					{
						m_uiRootFrameGroup.push(item);
					}
					else
					{
						for(i = 0;i<m_componentGroup.length;i++)
						{
							var citem:ComponentItem = m_componentGroup[i];
							
							if(citem.item == compon.parent)
							{
								item.parent = citem;
								
								break;
							}
						}
					}
					
					m_componentGroup.push(item);
					
					currTool.target = compon;
					m_targetComMoveControl.target = compon;
					toolInit();
					
					compon.addEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent);
					
					ObserverEvt.Send(OpenCompoentInfoMedCMD.OPENCOMPENTINFOMED,item);
					ObserverEvt.Send(ROOTFRAMEGROUPINFO,m_uiRootFrameGroup);
				}
			}
		}
		
		private var m_rootItemPoint : int;
		
		/**
		 * 打开界面XML完成事件
		 * @param evt 
		 */		
		public function openUIXML(vaule : Object) : void
		{
			var uiFrame : UIFrame = vaule["uiFrame"] as UIFrame;
			
			var rootItem : ComponentItem = vaule["rootItem"];
			
			m_uiLayer.addChild(uiFrame);
			
			m_uiRootFrameGroup.push(rootItem);
			checkAddComponent(rootItem);
			
			ObserverEvt.Send(ROOTFRAMEGROUPINFO,m_uiRootFrameGroup);
			
			for each(var item : DisplayObject in uiFrame.collection)
			{
				item.addEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent,false,0,true);
			}
			
			uiFrame.addEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent,false,0,true);
		}
		
		/**
		 * 递归检测ComponentItem和添加到m_componentGroup
		 * @param item
		 */		
		private function checkAddComponent(item : ComponentItem) : void
		{
			m_componentGroup.push(item);
			
			for(var i:int=0;i<item.childs.length;i++)
			{
				m_componentGroup.push(item.childs[i]);
				
				if(item.childs[i].childs.length > 0)
				{
					checkAddComponent(item.childs[i]);
				}
			}
		}
		
		private function toolInit():void
		{
			currTool.parent.setChildIndex(currTool, currTool.parent.numChildren - 1);
		}
		
		/**
		 * 清除选中目标 
		 */		
		public function clearSelectTarget() : void
		{
			currTool.target = null;
			m_targetComMoveControl.target = null;
		}
		
		/**
		 * 设定选中皮肤
		 * @param evt 
		 */	
		public function setSelectSkin(value : Object) : void
		{
			var image : Image = currTool.target as Image;
			
			if(!image) return;
			
			var data : Object = value;
			
			image.skinName = data["skin"];
		}
		
		/**
		 * 删除组件 
		 */		
		public function removeComponent() : void
		{
			var item:ComponentItem;
			
			if(currTool.target)
			{
				if(currTool.target == m_uiLayer)  
				{
					return;
				}
				//====================================================
				for(var j:int =0;j<m_uiRootFrameGroup.length;j++)
				{
					item = m_uiRootFrameGroup[j];
						
					if(item.item == currTool.target)
					{
						m_uiRootFrameGroup.splice(j,1);
						break;
					}
				}
				
				for(var i:int=0;i<m_componentGroup.length;i++)
				{
					item = m_componentGroup[i];
						
					if(item.item == currTool.target)
					{
						m_componentGroup.splice(i,1);
						item.parent = null;
						break;
					}
				}
					
				if(item.item)
				{
					item.item.removeEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent);
				}

				//删除组件的view
				if(currTool.target.parent)
				{
					currTool.target.parent.removeChild(currTool.target);
				}
				
				clearSelectTarget();
				
				m_uiLayer.width = m_uicontain.width;
				m_uiLayer.height = m_uicontain.height;
				m_uiLayer.drawBckgound();
				
				ObserverEvt.Send(ROOTFRAMEGROUPINFO,m_uiRootFrameGroup);
			}
		}
		
		/**
		 * 收到保存界面XML完成事件
		 * @param evt 
		 */		
		private function onRecSaveUIXMLComplete(evt : GEevnt) : void
		{
			
		}
		
		/**
		 * 添加组件
		 * @param value 
		 */	
		private function addComponent(item : ComponentItem,parentItem : ComponentItem=null) : void
		{
			if(parentItem)
			{
				item.parent = parentItem;
			}
			
			item.item.addEventListener(MouseEvent.MOUSE_DOWN,onSelectComponent,false,0,true);
		}
		
		private function updateItem(evt : Event) : void
		{
			m_uicontain.update();
			
			if(currTool.target)
			{
				ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
			}
		}
		
		/**
		 * 改变选中组建信息
		 */		
		public function updateComProperty( ) : void
		{
			currTool.update();
		}
		
		
		/**
		 * 打开界面xml
		 * @param evt 
		 */	
		public function openUIXMLByBrower( ) : void
		{
			var content : Object = new Object;
			
			content["componentGroup"] = m_componentGroup;
			
			ObserverEvt.Send(OpenUIXMLCMD.OPENUIXML,content);
		}
		
		/**
		 * 保存界面xml
		 * @param event 
		 */		
		public function saveBtnByBrower( ) : void
		{
			ObserverEvt.Send(SaveUIXMLCMD.SAVEUIXML,m_uiRootFrameGroup);
		}
		
		private function onKeyMove(evt : GEevnt) : void
		{
			currTool.dragTarget();
		}
		
		/**
		 * 设置工具对象 
		 */		
		public function setOperTarget(value : ComponentItem) : void
		{
			currTool.target = value.item;
			m_targetComMoveControl.target = value.item;
			
			toolInit();
			ObserverEvt.Send(OpenCompoentInfoMedCMD.OPENCOMPENTINFOMED,value);
		}
		
		/**
		 * 获取UI面板组
		 * @return  
		 */		
		public function get uiRootFrameGroup() : Vector.<ComponentItem>
		{
			return m_uiRootFrameGroup;
		}
		
		/**
		 * 设置被控制的组建是否锁定
		 * @param value 
		 */		
		public function set compoentControlLockedStatu(value : Boolean) : void
		{
			m_targetComMoveControl.locked = value;
		}
		
		/**
		 * 
		 * @param evt 
		 */		
		private function onMouseOverUIContain(evt : MouseEvent) : void
		{
			m_targetComMoveControl.locked =false;	
		}
		
		/**
		 * 
		 * @param evt 
		 */		
		private function onMouseOutUIContain(evt : MouseEvent) : void
		{
			m_targetComMoveControl.locked = true;
		}
	}
}